<?php

namespace app\leadertest\controller;

use app\common\controller\AdminBase;
use app\common\model\Paylog;
use app\common\model\Statis;
use think\Session;

class Index extends AdminBase
{
    /*
    * 后台首页（统计）
    *
    *
    * */
    public function testss()
    {

        var_dump(Session::get());

    }

    public function index()
    {
        $sysoemid = Session::get('sysloginid');
        $mapall = array('in', $this->getsysoem_mid($sysoemid));
        $paylog = new Paylog();
        //当日交易统计
        $day = strtotime(date('Y-m-d'));
        $day2 = strtotime(date('Y-m-d', strtotime('+1 day')));
        $map['mid'] = $mapall;
        $map['add_time'] = array('between', '' . $day . ',' . $day2 . '');
        $map['pay_statu'] = 1;
        $paylog5 = $paylog->where($map)->order('id desc')->limit('6')->select();
        $ordercount = $paylog->where($map)->count();
        $pointcount = $paylog->where($map)->sum('price');
        $machcount = $paylog->where($map)->count('Distinct mid');
        //本周交易统计
        if (date('w') == 1) {
            $week_start = strtotime(date('Y-m-d'));
        } else {
            $week_start = strtotime('last monday');
        }
        if (date('w') == 0) {
            $week_end = strtotime(date('Y-m-d'));
        } else {
            $week_end = strtotime("next sunday");
        }
        $map2['add_time'] = array('between', '' . $week_start . ',' . $week_end . '');
        $map2['pay_statu'] = 1;
        $map2['mid'] = $mapall;

        $pointweekcount = $paylog->where($map2)->sum('price');
        //本月交易统计
        $mon_start = strtotime(date("Y-m-01"));
        $BeginDate = date('Y-m-01', strtotime(date("Y-m-d")));
        $mon_end = strtotime("$BeginDate +1 month -1 day");
        $map3['add_time'] = array('between', '' . $mon_start . ',' . $mon_end . '');
        $map3['pay_statu'] = 1;
        $map3['mid'] = $mapall;

        $pointmoncount = $paylog->where($map3)->sum('price');
        //本年度交易统计
        $year_start = strtotime(date("Y-01-01"));
        $year_end = strtotime("+1 year", $year_start) - 1;
        $map4['add_time'] = array('between', '' . $year_start . ',' . $year_end . '');
        $map4['pay_statu'] = 1;
        $map4['mid'] = $mapall;

        $pointyearcount = $paylog->where($map4)->sum('price');
        //月度详细交易统计
        $days = strtotime(date('Y-m-d'));
        $days2 = strtotime(date('Y-m-d', strtotime('-7 day')));
        $tj['add_time'] = array('between', [$days2, $days]);
        $tj['mid'] = array('in', $this->getsysoem_mid($sysoemid));
        $tj['pay_statu'] = 1;
        for($i=7;$i>=1;$i--) {
            $day1=strtotime(date('Y-m-d', strtotime('-'.$i.' day')));
            $day2=$day1+86399;
            $dates[] =date('Y-m-d',$day1);
            $tj['add_time']=array('between', [$day1, $day2]);
            $order[] = $paylog->where($tj)->count();
            $price[] = $paylog->where($tj)->sum('price');
        }
        if (isset($dates)) {
            $dated = array_merge(array_unique($dates));
        } else {
            $datad = '';
        }
        $tongji2 = array('riqi' => json_encode($dated), 'order' => json_encode($order), 'price' => json_encode($price));
        $this->assign('tongji2', $tongji2);
//        var_dump($dates);
//        exit();
//        $Statis = new Statis();
//        $tj['dtime'] = array('between', [$days2, $days]);
//        $tongji = $Statis->where($tj)->order('dtime')->select();
//        $tongji = collection($tongji)->toArray();
//        if ($tongji) {
//            foreach ($tongji as $k => $v) {
//                $dates[] = date('Y-m-d', $v['dtime']);
//                $order[] = $v['orders'];
//                $price[] = $v['price'];
//            }
//            if (isset($dates)) {
//                $dated = array_merge(array_unique($dates));
//            } else {
//                $datad = '';
//            }
//            $tongji2 = array('riqi' => json_encode($dated), 'order' => json_encode($order), 'price' => json_encode($price));
//            $this->assign('tongji2', $tongji2);
//        } else {
//            $tongji2 = array('riqi' => json_encode(''), 'order' => json_encode(''), 'price' => json_encode(''));
//            $this->assign('tongji2', $tongji2);
//        }
        $this->assign('pointyearcount', number_format($pointyearcount, 2));
        $this->assign('pointmoncount', number_format($pointmoncount, 2));
        $this->assign('pointweekcount', number_format($pointweekcount, 2));
        $this->assign('machcount', $machcount);
        $this->assign('ordercount', $ordercount);
        $this->assign('pointcount', number_format($pointcount, 2));
        $this->assign('paylog5', $paylog5);
        return $this->fetch();
    }
}
